package com.xd.service.impl;


import com.github.pagehelper.PageHelper;
import com.xd.bean.ManInfo;
import com.xd.bean.PersonInfo;
import com.xd.bean.Record;
import com.xd.mapper.PersonInfoMapper;
import com.xd.service.IPersonInfoService;
import com.xd.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PersonInfoService implements IPersonInfoService {
	@Autowired
	private PersonInfoMapper personInfoMapper;//这里会报错，但是并不会影响

	//事务注解
	@Transactional
	@Override
	public Integer addInfo(PersonInfo personInfo) throws  Exception{
		/*personInfo.setId(Tools.getUUID());
		Integer r=personInfoMapper.addInfo(personInfo);*/
		personInfo.setId("123");
		personInfoMapper.addInfo(personInfo);

		PersonInfo personInfo1=new PersonInfo();
		personInfo1.setId("1");
		personInfoMapper.addInfo(personInfo1);
		return 1;
	}

	@Transactional
	@Override
	public Integer addMan(ManInfo manInfo,Record record) {
		Integer r=0;
		//判断手机号是否存在
		Map<String,String> map = new HashMap<String,String>();
		map.put("tel",manInfo.getTel());
		map.put("comePage",manInfo.getComePage());
		int num=personInfoMapper.checkTelIsExist(map);
		if(num>0){
			return -1;
		}
		//String mid=Tools.getUUID();
		//manInfo.setId(mid);
		String mid=manInfo.getId();

		r=personInfoMapper.addMan(manInfo);


		if(Tools.notEmpty(manInfo.getCallBackUrl())){
			//存解析的callback url
			record.setId(mid);
			personInfoMapper.addManRecord(record);
		}

		return r;
	}
	@Override
	public List<Map<String,Object>> getInfoList(String page,String startTime,String endTime) {
		Map<String,String> map = new HashMap<String,String>();
		if(Tools.notEmpty(startTime)){
			map.put("startTime",startTime);
		}
		if(Tools.notEmpty(endTime)){
			map.put("endTime",endTime);
		}

		map.put("comePage",page);
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		list=personInfoMapper.getInfoList(map);
		return list;
	}


	@Override
	public  Integer updateStatus(String status,String msg,String id){
		Map<String,String> map = new HashMap<String,String>();
		map.put("status",status);
		map.put("msg",msg);
		map.put("id",id);
		return personInfoMapper.updateStatus(map);
	}

	/*
    * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
    * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
    * pageNum 开始页数
    * pageSize 每页显示的数据条数
    * */
	//@Override
	//public List<User> findAllUser(int pageNum, int pageSize) {
		//将参数传给这个方法就可以实现物理分页了，非常简单。
	//	PageHelper.startPage(pageNum, pageSize);
	//	return userMapper.selectAllUser();
	//}



}
